const { By, until } = require('selenium-webdriver')

module.exports =  function getDownloadCount (driver, appid) {
  const target = By.css('#down-trend .des .con:first-child')
  let currentEle  
  return driver.get(`https://www.qimai.cn/andapp/downTotal/appid/${appid}`)
  .then(() => {
    return driver.wait(until.elementLocated(target))
  })
  .then(() => {
    return driver.sleep(2000)
  })
  .then(() => {
    const webEle = driver.findElement(target)
    currentEle = webEle
    return driver.wait(until.elementTextMatches(webEle, /\d+/))
  })
  .then(() => {
    return currentEle.getText()
  })
  .then((count) => {
    console.log(count)
    return {
      count: parseInt(count.split('').filter((ch) => !!ch.match(/\d/)).join(''))
    }
  })
}